package com.nine.pesys.util.common;

import com.nine.pesys.model.entity.User;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;

/**
 * 分页工具类
 * @author root
 *
 * @param <T>
 */
public class PageData<T> implements Serializable {
	public static final int DEFAULT_PAGESIZE = 3;
	private int pageNo = 1; //当前页码
	private int pageSize = DEFAULT_PAGESIZE; //每页信息条数
	private int totalCount = 0; //最大记录数
	private int totalPageCount = 0; //最大页数
	private Collection<T> datas = Collections.emptyList(); //结果集

	public PageData() {
	}
	
	public PageData(int totalCount, int pageNo, int pageSize) {
		if (pageNo > 0) {
			this.pageNo = pageNo;
		}
		this.totalCount = totalCount;
		this.pageSize = pageSize;
		this.init();
	}
	
	public PageData(int totalCount, Collection<T> datas) {
		this.totalCount = totalCount;
		if (datas != null && !datas.isEmpty()) {
			this.datas = datas;
		}
		this.init();
	}
	
	public PageData(int totalCount, int pageNo, Collection<T> datas) {
		if (pageNo > 0) {
			this.pageNo = pageNo;
		}
//        if(pageNo>totalPageCount){
//           this.pageNo=totalCount;
//        }
        if (pageNo<=0){
            this.pageNo = pageNo;
        }
		this.totalCount = totalCount;
		if (datas != null && !datas.isEmpty()) {
			this.datas = datas;
		}
		this.init();
	}

	public PageData(int totalCount, int pageNo, int pageSize, Collection<T> datas) {
        if (pageNo > 0) {
            this.pageNo = pageNo;
        }
        if (pageNo<=0){
            this.pageNo = 1;
        }
		this.totalCount = totalCount;
		this.pageSize = pageSize;
		if (datas != null && !datas.isEmpty()) {
			this.datas = datas;
		}
		this.init();
	}

	public PageData<T> init() {
		if (totalCount == 0) {
			return this;
		}

		if (totalCount % pageSize == 0) {
			this.totalPageCount = totalCount / pageSize;
		} else {
			this.totalPageCount = totalCount / pageSize + 1;
		}

		//当前页码大于最大分页数据
		if (pageNo > totalPageCount) {
			this.pageNo = totalPageCount;
		}
		
		return this;
	}
	
	public PageData<T> init(Collection<T> datas) {
		if (datas != null && !datas.isEmpty()) {
			this.datas = datas;
		}
		this.init();
		return this;
	}
	
	/**
	 * 分页下标
	 * @return
	 */
	public int getStartIndex() {
		return (pageNo - 1) * pageSize;
	}


	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getTotalPageCount() {
		return totalPageCount;
	}

	public void setTotalPageCount(int totalPageCount) {
		this.totalPageCount = totalPageCount;
	}

	public Collection<T> getDatas() {
		return datas;
	}

	public void setDatas(Collection<T> datas) {
		this.datas = datas;
	}

	public static void main(String [] args) {
		PageData<User> pd = new PageData<User>(52, 1, 10, null);
		System.out.println(pd);
	}

    @Override
    public String toString() {
        return "PageData{" +
                "pageNo=" + pageNo +
                ", pageSize=" + pageSize +
                ", totalCount=" + totalCount +
                ", totalPageCount=" + totalPageCount +
                ", datas=" + datas +
                '}';
    }
}
